package com.fit.dao.coach;

import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.coach.Coach;



public class CoachDAOImpl implements CoachDAO{

	@Inject
	private Session session;

	@Log
	public boolean save(Coach newCoach) {
		if (newCoach != null) {
			session.save(newCoach);
			return true;
		}
		return false;
	}

	@Log
	public boolean delete(Coach coach) {
		if (coach != null) {
			session.delete(coach);
			return true;
		}
		return false;
	}

	@Log
	public boolean update(Coach coach) {
		if (coach != null) {
			session.update(coach);
			return true;
		}
		return false;
	}

	@Log
	public List findAll() {
		return session.createCriteria(Coach.class).list();
	}

	@Log
	public boolean deleteByID(Long id) {
		Coach u = findByID(id);
		if (u != null) {
			delete(u);
			return true;
		}
		return false;
	}

	@Log
	public Coach findByID(Long id) {
		if (id != null) {
			return (Coach) session.get(Coach.class, id);
		}
		return null;
	}

	@Log
	public Coach findByNameAndSurname(String name, String surname) {
		if (surname == null || name == null) {
			return null;
		}

		if (surname.equals("") || name.equals("")) {
			return null;
		}

		List l = session.createCriteria(Coach.class)
				.add(Restrictions.eq("name", name))
				.add(Restrictions.eq("surname", surname)).list();
		Coach u = null;
		if (l != null) {
			if (l.size() > 0) {
				u = (Coach) l.get(0);
			}
		}
		return u;
	}

	

}
